package com.example.shop_2211.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2211.MainActivity
import com.example.shop_2211.R
import com.example.shop_2211.databinding.ActivityLoginBinding
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun initView() {
        binding.btnLogin.setOnClickListener {
            val etPhone = binding.etPhone.text.toString()
            val etPwd = binding.etPwd.text.toString()
            if(etPwd.isEmpty()||etPhone.isEmpty()){
                ToastUtils.showLong("用户名和密码不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(etPhone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            if(!RegexUtils.isMatch("^[\\w]{6,20}$",etPwd)){
                ToastUtils.showLong("密码格式不正确")
                return@setOnClickListener
            }
            viewModel.login(etPhone,etPwd)
        }
        binding.btnGologin.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                //不带参数的路由跳转
                ARouter.getInstance().build("/app/MainActivity").navigation()
            }
        }
        viewModel.loginFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_login

}